<template>
  <div class="addDevice">
    <h3 class="title">添加设备信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
     <el-form-item label="操作人">
        <el-input :disabled = true :placeholder="this.username"/>
      </el-form-item>
      <el-form-item label="设备名" prop="deviceName">
        <el-input v-model="ruleForm.deviceName"></el-input>
      </el-form-item>
      <el-form-item label="数量	" prop="num">
        <el-input v-model="ruleForm.num"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')">添加</el-button>
        <el-button type="info" :plain="true" @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import userRequest from "../request/user";
export default {
  props: ["clearComp"],
  data() {
     var deviceName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写所购设备"));
      } else {
        return callback();
      }
    };
   
    var num = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写所购设备"));
      } else {
        return callback();
      }
    };
  
  
    return {
      userinfo: null,
      username: "",
      ruleForm: {
        deviceName: "",
        num: "",
      },
      rules: {
        deviceName: [{ validator: deviceName, trigger: "blur" }],
        num: [{ validator: num, trigger: "blur" }],
      }
    };
  },

  methods: {
    // 添加客户
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          userRequest
            .addDevice({
              deviceName: this.ruleForm.deviceName,
              num: this.ruleForm.num,
              name: this.username,
            })
            .then(res => {
              if (res.data.success == true) {
                this.$message.success("添加成功!");
                this.resetForm();
                this.clearComp();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
    // 清空表单数据
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  },

  created() {
    // 获取本地用户信息
    this.userinfo = JSON.parse(window.localStorage.getItem("userinfo"));
     if (this.userinfo) {
      this.username = this.userinfo.userName;
    }
  }
};
</script>

<style lang="scss" scoped>
.addDevice {
  overflow: hidden;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    display: flex;
    margin-top: 10px;
    height: 50px;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 11px;
  }
}
</style>